package com.wpml.product.controller;

import com.wpml.common.util.PageUtils;
import com.wpml.common.util.ResultJson;
import com.wpml.product.entity.ProductAttrValueModel;
import com.wpml.product.service.AttrService;
import com.wpml.product.service.ProductAttrValueService;
import com.wpml.product.vo.AttrRespVo;
import com.wpml.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @date 2019-10-01 22:50:32
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    // /product/attr/base/listforspu/{spuId}
    @GetMapping("/base/listforspu/{spuId}")
    public ResultJson baseAttrlistforspu(@PathVariable("spuId") Long spuId) {

        List<ProductAttrValueModel> entities = productAttrValueService.baseAttrlistforspu(spuId);

        return ResultJson.ok(entities);
    }

    ///product/attr/base/list/{catelogId}
    @GetMapping("/{attrType}/list/{catelogId}")
    public ResultJson baseAttrList(@RequestParam Map<String, Object> params,
                                   @PathVariable("catelogId") Long catelogId,
                                   @PathVariable("attrType") String type) {

        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, type);
        return ResultJson.ok(page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public ResultJson list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return ResultJson.ok(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public ResultJson info(@PathVariable("attrId") Long attrId) {
        AttrRespVo respVo = attrService.getAttrInfo(attrId);

        return ResultJson.ok(respVo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public ResultJson save(@RequestBody AttrVo attr) {
        attrService.saveAttr(attr);

        return ResultJson.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public ResultJson update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);

        return ResultJson.ok();
    }

    ///product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    public ResultJson updateSpuAttr(@PathVariable("spuId") Long spuId,
                                    @RequestBody List<ProductAttrValueModel> entities) {

        productAttrValueService.updateSpuAttr(spuId, entities);

        return ResultJson.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public ResultJson delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return ResultJson.ok();
    }

}
